
import tkinter as tk ,random,copy,webbrowser
import threading as thd,time
# 组合随机抽取列表
value_27=[
    111,112,113,
    121,122,123,
    131,132,133,
    211,212,213,
    221,222,223,
    231,232,233,
    311,312,313,
    321,322,323,
    331,332,333
    ]
class ui_windows(tk.Frame):                                 # 继承自 tk.Tk
    def __init__(self,parent=None):
        super().__init__(parent)  
        #统一字体和大小----------------------------
        self.Font0='隶书'
        self.FontSize=9
        # 按钮状态
        self.bk_press=0
        #
        self.img_result={}
        for i in value_27 :
            try :
                self.img_result[i]=tk.PhotoImage(file=f'img2\\{i}.png')
            except Exception as err:
                self.img_result[i]=tk.PhotoImage(file=r'img2\\000.png')
                print(err)
        # 解签画布图片
        self.img_result_0=tk.PhotoImage(file=r'img2\\000.png')
        self.img_qq_ing=tk.PhotoImage(file=r'img2\\001.png')
        self.result_canvas=''
        #
        self.photo1=tk.PhotoImage(file=r'img/1.png')
        self.photo2=tk.PhotoImage(file=r'img/2.png')
        self.photo3=tk.PhotoImage(file=r'img/3.png')
        # 杯的三种可能（图片）列表
        self.value=[self.photo1,self.photo2,self.photo3]
        self.text0,self.text1,self.text2='','',''
        self.value_text=[self.text0,self.text1,self.text2]
        self.value_text_x=[150,300,450]
        # 杯图片值
        self.img0,self.img1,self.img2='','',''
        self.value_img=[self.img0,self.img1,self.img2]
        self.value_img_x=[100+49,250+49,400+49]
        self.value_c=['圣','笑','阴']
        # 3个画布列表
        self.value_canvas=['canvas1','canvas2','canvas3']
        #
        self.photo4=tk.PhotoImage(file=r'img/bg2.png')
        # 抽签按钮图片
        self.photo_cq=tk.PhotoImage(file=r'img/cq.png')
        self.photo_cq2=tk.PhotoImage(file=r'img/cq2.png')
        self.bk_cq=['','','']
        # 画布文本列表
        self.text_field=['','']
        # 背景图片
        self.photo=tk.PhotoImage(file=r'img/bg.png')
        self.place(x=0,y=0,height=self.photo.height(),width=self.photo.width())
        # 背景画布
        self.canvas=tk.Canvas(self,width=self.photo.width(),height=self.photo.height(),
                              highlightthickness=0,
                            #   bg='#1b1b1b'
                              )
        # 画布背景图
        self.canvas.create_image(self.photo.width()/2,self.photo.height()/2,image=self.photo)
        self.canvas.place(x=0,y=0)    
        # 关于
        self.canvas.create_text(490,590,
                                text='icsteam.cn',
                                font=(self.Font0,12),
                                anchor='w',
                                fill='#707070',
                                tags='about'
                                )
        self.arrow('about')
        self.canvas.tag_bind('about','<Button-1>',lambda envent:webbrowser.open('www.icsteam.cn'))
        self.ui_main() 
        # self.canvas.configure(cursor='hand2')

    # 取消 
    def view_text(self,data_text):
        self.canvas.delete(self.text_field[0])
        self.text_field[0]=self.canvas.create_text(68+480,420,
                                width=500,
                                text=data_text,
                                anchor='e',
                                justify='left',
                                # fill='red',
                                font=(self.Font0,15))        
        pass
    # 抽签按钮点击运行该方法  
    def bk_enter(self,event):
        self.canvas.delete(self.bk_cq[0])
        self.bk_cq[1]=self.canvas.create_image(519,26,
                                            image=self.photo_cq2,
                                            tags='bk1',
                                            anchor=tk.NW)
        self.bk_press=1 # 按钮按下状态为1
        self.canvas.tag_bind('bk1','<Enter>',self.bk_leave)    
    def bk_leave(self,event):
        self.canvas.delete(self.bk_cq[1])
        self.bk_cq[0]=self.canvas.create_image(519,26,
                                            image=self.photo_cq,
                                            tags='bk0',
                                            anchor=tk.NW) 
    def arrow(self,tags):
        self.canvas.tag_bind(tags,'<Enter>',self.bk_enter_cq)
        self.canvas.tag_bind(tags,'<Leave>',self.bk_leave_cq)
    # 鼠标指针移入时显示手形                     
    def bk_enter_cq(self,event):
        self.canvas.configure(cursor='hand2')
    # 鼠标指针离开时显示标准箭头型
    def bk_leave_cq(self,event):
        self.canvas.configure(cursor='arrow')    
    def ui_main(self):    
        #tkinter变量------------------------------
        # self.tag_port=tk.StringVar()                     #获取用户输入的端口号         
        # self.tag_addrh=tk.StringVar()                    #获取用户输入的结束地址
        # self.tag_addrl=tk.StringVar()                    #起始地址
        # self.tag_code=tk.StringVar()                     #获取用户输入的功能码
        # self.tag_rwaddr=tk.StringVar()                   #获取用户输入的读写地址
        # self.tag_num=tk.StringVar()                      #获取用户输入的读写数量
        # self.tag_display=tk.StringVar()
        # self.tag_load=tk.IntVar()    
        
        # 容器4抽签按钮
        self.bk_cq[0]=self.canvas.create_image(519,26,
                                            image=self.photo_cq,
                                            tags='bk0',
                                            anchor=tk.NW)
        # 点击事件
        self.arrow('bk0')
        self.canvas.tag_bind('bk0','<Button-1>',self.bk_enter)
        #  must be n, ne, e, se, s, sw, w, nw, or center
        text=self.canvas.create_text(25,38,text='博杯程序',font=(self.Font0,25),anchor='w')    
        self.canvas.create_text(42,573,text='注：心诚则灵，无需拘泥于形式',font=(self.Font0,12),anchor='w') 
        # 画一个矩形 create_rectangle(x1,y1,x2,y2,fill='red')
        # 解签画布
        self.result_canvas=self.canvas.create_image(300,411.5,
                                                    image=self.img_result_0
                                                        )
        # 杯展示---
        self.value_img[0]=self.canvas.create_image(49+100,45.5+120,image=self.photo4)
        self.value_img[1]=self.canvas.create_image(49+250,45.5+120,image=self.photo4)
        self.value_img[2]=self.canvas.create_image(49+400,45.5+120,image=self.photo4)

        # 三圣杯图片文字描述---
        self.value_text[0]=self.canvas.create_text(150,240,text='空',font=(self.Font0,22),anchor='center')
        self.value_text[1]=self.canvas.create_text(300,240,text='空',font=(self.Font0,22),anchor='center')
        self.value_text[2]=self.canvas.create_text(450,240,text='空',font=(self.Font0,22),anchor='center')        
#ttkb.Window
class MainWindows(tk.Tk):
    def __init__ (self):
        super().__init__()
        # ttkb.Style("darkly")
        # cosmo morph
        # ttkb.Style('morph')
        self.wd=ui_windows(self)
    def main(self):
        pass     
not_en=[0,0,0]      # 停止图片循环播放标识，为1时停止
fb_list=[0,0,0]     # 图片停止播放反馈
# 循环播放图片和文字
def run(self,num):
    global not_en,fb_list
    while 1:
        # 停止播放信号
        if not_en[num] == 1 :
             fb_list[num]=1 #停止播放反馈
             break          
        for i in range(3):
                    # 随机数
                    rdm_num=random.randint(0,2)
                    # 播放图片
                    # root.wd.canvas.delete(root.wd.value_img[num])
                    root.wd.value_img[num]=root.wd.canvas.create_image(root.wd.value_img_x[num],45.5+120,image=root.wd.value[rdm_num])
                    # 播放文字
                    root.wd.canvas.delete(root.wd.value_text[num])
                    root.wd.value_text[num]=root.wd.canvas.create_text(root.wd.value_text_x[num],240,text=root.wd.value_c[rdm_num],font=(root.wd.Font0,22),anchor='center')
                    time.sleep(0.3) #播放间隔300ms
# 二十七签随机抽取，返回列表和值
def cal_3b(list):
    num_list=[]
    k=time.time()
    random.seed(k)    
    tmp0=random.choice(list)
    tmp1= str(copy.copy(tmp0))
    for i in tmp1 :
        num_list.append(int(i))
    return num_list,tmp0
# 主程序
def main_run():
    global img,not_en,value_27,fb_list
    thd_list=['','','']
    for i in range(3) :
        not_en[i]=0
        fb_list[i]=0
    for j in range(3):
        thd_list[j]=thd.Thread(target=run,args=('',j))
        thd_list[j].daemon=1
        thd_list[j].start()
    root.wd.result_canvas=root.wd.canvas.create_image(300,411.5,
                                                      image=root.wd.img_qq_ing
                                                      )
    num1=0
    while 1:
        if num1 >= 3 :
            # 抽签
            a,b=cal_3b(value_27)
            print(a,b)
            for i in range(3):
                not_en[i]=1
                while 1:
                    if fb_list[i] == 1 :
                        # root.wd.canvas.delete(root.wd.value_img[i])
                        root.wd.value_img[i]=root.wd.canvas.create_image(root.wd.value_img_x[i],45.5+120,image=root.wd.value[a[i]-1])
                        root.wd.canvas.delete(root.wd.value_text[i])
                        root.wd.value_text[i]=root.wd.canvas.create_text(root.wd.value_text_x[i],240,text=root.wd.value_c[a[i]-1],font=(root.wd.Font0,22),anchor='center')
                        break
                    time.sleep(0.3)
                time.sleep(0.5)
            # root.wd.canvas.delete(root.wd.result_canvas)
            root.wd.result_canvas=root.wd.canvas.create_image(300,411.5,
                                                              image=root.wd.img_result[b]
                                                              )
            break
        num1+=1
        print(num1)
        time.sleep(1)
first_scan=0                 
def main():
    global first_scan
    while 1:
        if root.wd.bk_press == 1:
            # 主程序线程
            thd0=thd.Thread(target=main_run)
            thd0.daemon=1
            thd0.start()
            root.wd.bk_press=0
        time.sleep(0.5)
if __name__ == '__main__':
    root=MainWindows()
    # 主程序
    thd0=thd.Thread(target=main)
    thd0.daemon=1
    thd0.start()
    wh=root.winfo_screenheight()
    ww=root.winfo_screenwidth()
    mwh=611
    mww=599
    x=(ww-mww)/2
    y=(wh-mwh)/2
    root.geometry("%dx%d+%d+%d"%(mww,mwh,x,y))            
    root.resizable(width=0, height=0)                #限制窗口最大化
    root.iconphoto(False, tk.PhotoImage(file=r'./img/000.png'))
    root.title("博杯")  
    root.mainloop()